<template>
  <div>
    <el-form ref="form" label-width="80px">
      <!-- SPU名称 -->
      <el-form-item label="SPU名称">
        {{ spuName }}
      </el-form-item>
      <!-- SKU名称 -->
      <el-form-item label="SKU名称">
        <el-input v-model="skuInfo.skuName" placeholder="sku名称" />
      </el-form-item>
      <!-- 价格(元) -->
      <el-form-item label="价格(元)">
        <el-input v-model="skuInfo.price" type="number" />
      </el-form-item>
      <!-- 重量(千克) -->
      <el-form-item label="重量(千克)">
        <el-input v-model="skuInfo.weight" placeholder="重量(千克)" />
      </el-form-item>
      <!-- 规格描述 -->
      <el-form-item label="规格描述">
        <el-input v-model="skuInfo.skuDesc" type="textarea" rows="4" />
      </el-form-item>
      <!-- 平台属性 -->
      <el-form-item label="平台属性">
        <el-form label-width="80px" :inline="true">
          <el-form-item v-for=" attr in AttrInfoList" :key="attr.id" :label="attr.attrName">
            <el-select v-model="attr.attrIdAndAttrValueId" placeholder="请选择">
              <el-option v-for="attrValue in attr.attrValueList" :key="attrValue.id" :label="attrValue.valueName" :value="`${attr.id}:${attrValue.id}`" />
            </el-select>
          </el-form-item>

        </el-form>
      </el-form-item>
      <!-- 销售属性 -->
      <el-form-item label="销售属性">
        <el-form :inline="true" label-width="80px">
          <el-form-item v-for="sal in SpuSaleAttrList " :key="sal.id" :label="sal.saleAttrName">
            <el-select v-model="sal.salIdAndSalValueId" placeholder="请选择">
              <el-option v-for="salValue in sal.spuSaleAttrValueList" :key="salValue.id" :label="salValue.saleAttrValueName" :value="`${sal.id}:${salValue.id}`" />
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <!-- 图片列表 -->
      <el-form-item label="图片列表">
        <template slot-scope="{row}">
          <el-table
            border
            :data="SpuImageLIst"
            style="width: 100%"
            @selection-change="selectionChange"
          >
            <el-table-column
              type="selection"
              label="label"
              width="80"
            />
            <el-table-column
              prop="prop"
              label="图片"
              width="width"
            >
              <template slot-scope="{row}">
                <img
                  style="width: 100px; height: 100px"
                  :src="row.imgUrl"
                >
              </template>
            </el-table-column>
            <el-table-column
              prop="imgName"
              label="名称"
              width="width"
            />
            <el-table-column
              prop="prop"
              label="操作"
              width="width"
            >
              <template slot-scope="{row}">
                <el-button v-if="row.isDefault === 0" type="primary" @click="setDefault(row)">设置默认</el-button>
                <el-button v-else type="success">默认</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-form-item>
      <!-- 保存和取消 -->
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'SKUForm',
  data() {
    return {
      // spu名字
      spuName: '',
      // 图片数据
      SpuImageLIst: [],
      // 平台属性的数据
      AttrInfoList: [],
      // 销售属性的数据
      SpuSaleAttrList: [],
      // sku数据
      skuInfo: {
        // 第一类收集的数据：父组件给的数据
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        // 第二类：需要通过数据双向绑定v-model收集
        skuName: '',
        price: 0,
        weight: '',
        skuDesc: '',
        // 第三类：需要自己书写代码
        // 默认图片
        skuDefaultImg: '',
        // 收集图片的字段
        skuImageList: [
        ],
        // 平台属性
        skuAttrValueList: [

        ],
        // 销售属性
        skuSaleAttrValueList: [
        ]

      },
      // 选中的图片
      imgList: []
    }
  },
  methods: {
    // 保存
    async save() {
      //  数据合并
      // 平台属性合并
      this.skuInfo.skuAttrValueList = this.AttrInfoList.reduce((arr, item) => {
        if (item.attrIdAndAttrValueId) {
          const [attrId, valueId] = item.attrIdAndAttrValueId.split(':')
          const obj = { attrId, valueId }
          arr.push(obj)
        }
        return arr
      }, [])
      // 合并销售属性
      this.skuInfo.skuSaleAttrValueList = this.SpuSaleAttrList.reduce((arr, item) => {
        if (item.salIdAndSalValueId) {
          const [saleAttrId, saleAttrValueId] = item.salIdAndSalValueId.split(':')
          const obj = { saleAttrId, saleAttrValueId }
          arr.push(obj)
        }
        return arr
      }, [])
      // 合并图片
      this.skuInfo.skuImageList = this.imgList.map(item => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id
        }
      })
      try {
        await this.$API.spu.reqAddSku(this.skuInfo)
        this.$message.success('添加成功')
        this.$emit('showStatus', 0)
      } catch (error) {
        console.log(error)
      }
    },
    // 取消
    cancel() {
      this.$emit('showStatus', 0)
      Object.assign(this._data, this.$options.data())
    },
    // 设置默认图片
    setDefault(row) {
      this.SpuImageLIst.forEach(item => {
        item.isDefault = 0
      })
      row.isDefault = 1
      this.skuInfo.skuDefaultImg = row.imgUrl
    },
    // 图片选中的事件
    selectionChange(val) {
      this.imgList = val
    },
    // 获取数据
    async  getData(category1Id, category2Id, row) {
      this.spuName = row.spuName
      this.skuInfo.category3Id = row.category3Id
      this.skuInfo.tmId = row.tmId
      this.skuInfo.spuId = row.id

      try {
        // 获取图片数据
        const res = await this.$API.spu.reqSpuImageLIst(row.id)
        const list = res.data
        list.forEach(item => {
          item.isDefault = 0
        })
        this.SpuImageLIst = list
        // 获取平台属性的数据
        const res1 = await this.$API.spu.reqAttrInfoList(category1Id, category2Id, row.category3Id)
        this.AttrInfoList = res1.data
        // 获取销售属性的数据
        const res2 = await this.$API.spu.reqSpuSaleAttrList(row.id)
        this.SpuSaleAttrList = res2.data
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
